package com.chen.order.controller;

import com.chen.model.dto.h5.OrderInfoDto;
import com.chen.model.entity.order.OrderInfo;
import com.chen.model.vo.common.Result;
import com.chen.model.vo.common.ResultCodeEnum;
import com.chen.model.vo.h5.TradeVo;
import com.chen.order.service.OrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cgj
 * @date 4/26/25 10:20 AM
 */
@RestController
@RequestMapping("/api/order/orderInfo/auth")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * /api/order/orderInfo/auth/updateOrderByOrderNo/{orderStatus}/{orderNo}
     * 修改订单状态
     */
    @PutMapping("updateOrderByOrderNo/{orderStatus}/{orderNo}")
    public void updateOrderByOrderNo(@PathVariable Integer orderStatus,
                                     @PathVariable String orderNo){

        orderService.updateOrderByOrderNo(orderStatus,orderNo);
    }
    /**
     * 根据orderNo查询订单信息
     * /api/order/orderInfo/auth/getOrderInfoByOrderNo/{orderNo}
     */
    @GetMapping("/getOrderInfoByOrderNo/{orderNo}")
    public OrderInfo getOrderInfoByOrderNo(@PathVariable String orderNo){
        return orderService.getOrderInfoByOrderNo(orderNo);
    }

    /**
     * 根据订单状态,查询订单信息, (我的订单)
     * http://localhost:8500/api/order/orderInfo/auth/1/10?orderStatus=
     */
    @GetMapping("{pageNum}/{pageSize}")
    public Result<PageInfo<OrderInfo> >  selectByOrderStatus(@PathVariable Integer pageNum,
                                                    @PathVariable Integer pageSize,
                                                    Integer orderStatus ){
        PageInfo pageInfo=orderService.selectByOrderStatus(pageNum,pageSize,orderStatus);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }
    /**
     * 根据订单id,获取订单信息
     * http://localhost:8500/api/order/orderInfo/auth/215
     */
    @GetMapping("/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable Long orderId){
        OrderInfo orderInfo = orderService.getOrderInfo(orderId);
        return Result.build(orderInfo,ResultCodeEnum.SUCCESS);
    }
    /**
     * 直接购买 不进入购物车
     http://localhost:8500/api/order/orderInfo/auth/buy/1
     */
    @GetMapping("buy/{skuId}")
    public Result<TradeVo> buy(@PathVariable Long skuId){
        TradeVo tradeVo=  orderService.buy(skuId);
        return Result.build(tradeVo,ResultCodeEnum.SUCCESS);
    }

    /**
     *
     http://localhost:8500/api/order/orderInfo/auth/submitOrder
     * 返回订单号(订单id)
     */
    @PostMapping("submitOrder")
    public Result<Long> submitOrder(@RequestBody OrderInfoDto orderInfoDto){
        Long orderId=orderService.submitOrder(orderInfoDto);
        return Result.build(orderId, ResultCodeEnum.SUCCESS);
    }


    /**
     *  http://localhost:8500/api/order/orderInfo/auth/trade
     *  结算页 ,返回购物车订单生成前准备的数据
     * @return
     */
    @GetMapping("/trade")
    public Result<TradeVo>  trade(){
        TradeVo tradeVo=orderService.trade();
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }
}
